package com.ego.product.controller;

import java.util.Arrays;

import com.ego.common.vo.BaseSearchVo;
import com.ego.common.vo.PageVo;
import com.ego.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.product.po.Brand;
import com.ego.product.service.BrandService;
import com.ego.common.util.Result;

import javax.validation.Valid;

/**
 * @author KamTang
 * @program: ego
 * @description 品牌控制器
 * @date 2022-11-13 12:56:14
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @PostMapping("/search")
    public Result search(@Valid @RequestBody BaseSearchVo search) {
        PageVo<Brand> page = brandService.search(search);
        return Result.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/get/{brandId}")
    public Result get(@PathVariable("brandId") Long brandId) {
        Brand brand = brandService.getById(brandId);
        return Result.ok().put("brand", brand);
    }

    /**
     * 信息
     */
    @GetMapping("/getVo/{brandId}")
    public Result getVo(@PathVariable("brandId") Long brandId) {
        BrandVo brandVo = brandService.getById(brandId).toVo();
        return Result.ok().put("brandVo", brandVo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@Valid @RequestBody Brand brand) {
        brandService.save(brand);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@Valid @RequestBody Brand brand) {
        brandService.updateCascade(brand);
        return Result.ok();
    }

    /**
     * 修改状态
     */
    @PostMapping("/update/status")
    public Result updateStatus(@Valid @RequestBody Brand brand) {
        brandService.updateById(brand);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@Valid @RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));
        return Result.ok();
    }

}
